﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class SQLhelper
    {
        static SqlConnection conn;
        SQLhelper()
        {
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        }
        public static DataSet GetQueryDataAdapter(string sql)
        {
            SqlDataAdapter ada = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();

            ada.Fill(ds);
            return ds;
        }
        public static DataSet GetDataSet(SqlCommand cmd)
        {
            if (string.IsNullOrEmpty(cmd.CommandText))
                return null;
            if (conn.State == System.Data.ConnectionState.Closed)
                conn.Open();
            cmd.Connection = conn;
            SqlDataAdapter ada = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            try
            { ada.Fill(ds);

            }
            catch(Exception ex)
            {
                throw ex;
            }
            return ds;
        }
        public static SqlDataReader ExcuteReader(SqlCommand cmd)
        {
            conn.Open();
            cmd.Connection = conn;
            SqlDataReader read;
            try
            {
                read = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return read;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
            
           public static int ExcuteSQL(SqlCommand cmd)
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.Connection = conn;
            int num = 0;
            try
            {
                num = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State == System.Data.ConnectionState.Open)
                    conn.Close();
            }
            return num;
        }
        public static object GetSca;
        }
    }

